package com.yiwenup.leetcode.top_interview_150;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-23 21:24:42
 * @Description https://leetcode.cn/problems/simplify-path/description/?envType=study-plan-v2&envId=top-interview-150
 */
public class No0071 {
    public String simplifyPath(String path) {
        Deque<String> queue = new ArrayDeque<>();
        String[] names = path.split("/");
        for (String name : names) {
            if (!name.isEmpty() && !".".equals(name) && !"..".equals(name)) {
                queue.addLast(name);
            }
            if ("..".equals(name) && !queue.isEmpty()) {
                queue.removeLast();
            }
        }
        if (queue.isEmpty()) {
            return "/";
        }
        StringBuilder sb = new StringBuilder();
        while (!queue.isEmpty()) {
            sb.append("/").append(queue.removeFirst());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(new No0071().simplifyPath("/home/user/Documents/../Pictures"));
    }
}
